package com.ruoyi.dp.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.dp.domain.DpFlightPlan;
import com.ruoyi.dp.mapper.DpFlightPlanMapper;
import com.ruoyi.dp.service.IDpFlightPlanService;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DpFlightPlanServiceImpl implements IDpFlightPlanService {

    private static final Logger log = LoggerFactory.getLogger(DpFlightPlanServiceImpl.class);

    @Autowired
    private DpFlightPlanMapper dpFlightPlanMapper;
    @Override
    public List<DpFlightPlan> selectDpFlightPlanList(DpFlightPlan dpFlightPlan) {
        return dpFlightPlanMapper.selectDpFlightPlanList(dpFlightPlan);
    }

    @Override
    public DpFlightPlan selectDpFlightPlanById(Long id) {
        return dpFlightPlanMapper.selectDpFlightPlanById(id);
    }

    @Override
    public int deleteDpFlightPlanById(Long id) {
        return dpFlightPlanMapper.deleteDpFlightPlanById(id);
    }

    @Override
    public int deleteDpFlightPlanByIds(Long[] ids) {
        return dpFlightPlanMapper.deleteDpFlightPlanByIds(ids);
    }

    @Override
    public int insertDpFlightPlan(DpFlightPlan dpFlightPlan) {
        return dpFlightPlanMapper.insertDpFlightPlan(dpFlightPlan);
    }

    @Override
    public int updateDpFlightPlan(DpFlightPlan dpFlightPlan) {
        return dpFlightPlanMapper.updateDpFlightPlan(dpFlightPlan);
    }

    @Override
    public String importUser(List<DpFlightPlan> dpFlightPlanList, Boolean isUpdateSupport) {
        if (StringUtils.isNull(dpFlightPlanList) || dpFlightPlanList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (DpFlightPlan dpFlightPlan :dpFlightPlanList){
            try {
                insertDpFlightPlan(dpFlightPlan);
                successNum++;
                successMsg.append("<br/>" + successNum + " 导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
